package test.administrator.example.com.mvptest1.framework.lce.impl.fragment;

import android.os.Bundle;
import android.view.View;

import test.administrator.example.com.mvptest1.framework.support.fragment.MvpFragment;
import test.administrator.example.com.mvptest1.framework.MvpPresenter;
import test.administrator.example.com.mvptest1.framework.lce.ILceAnimator;
import test.administrator.example.com.mvptest1.framework.lce.MvpLceView;
import test.administrator.example.com.mvptest1.framework.lce.impl.MvpLceViewImpl;

/**
 * Created by thinkpad on 2018/7/31.
 */

public class MvpLceFragment<D, V extends MvpLceView<D>, P extends MvpPresenter<V>>
		extends MvpFragment<V, P> implements MvpLceView<D> {

	// 初始化Lce UI布局（规定你的Lce布局文件的id）
	private MvpLceViewImpl<D> lceViewImpl;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (lceViewImpl == null) {
			lceViewImpl = new MvpLceViewImpl<D>();
		}
		initLceView(view);
	}

	private void initLceView(View v) {
		lceViewImpl.initView(v);
		lceViewImpl.setOnClickErrorListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onErrorClick();
			}
		});
	}

	public void setLceAnimator(ILceAnimator lceAnimator) {
		lceViewImpl.setAnimator(lceAnimator);
	}

	@Override
	public void showLoading(boolean pullToRefresh) {
		lceViewImpl.showLoading(pullToRefresh);
	}

	@Override
	public void showContent() {
		lceViewImpl.showContent();
	}

	@Override
	public void showError(Throwable e, boolean isPullToRefresh) {
		lceViewImpl.showError(e, isPullToRefresh);
	}

	@Override
	public void loadData(boolean pullToRefresh) {
		lceViewImpl.loadData(pullToRefresh);
	}

	@Override
	public void bindData(D data) {
		lceViewImpl.bindData(data);
	}

	public void onErrorClick() {
		loadData(false);
	}
}